//
//  XCStatusOriginalView.swift
//  小陈微博Swift用纯代码创建
//
//  Created by 小陈 on 16/3/2.
//  Copyright © 2016年 小陈. All rights reserved.
//

// 该类是用来处理 XCStatusCell 传过来的数据 并对XCStatusCell控件进行布局
import UIKit
import SnapKit
import SDWebImage



let XCStatusCellMargin : CGFloat = 8


class XCStatusOriginalView: UIView {
    
    // 
    var statusOriginalViewBottomConstraint: Constraint?
    
    
    var status:XCStatus?{
        didSet{
            

//            print(" picturesView.status ------> \( picturesView.status)")
            
            if let profile_image_url = status?.user?.profile_image_url{
                let url = NSURL(string: profile_image_url)
                IconView.sd_setImageWithURL(url, placeholderImage: UIImage(named: "avatar"))
            }
            
            // 设置用户名
            nameLable.text = status?.user?.screen_name
//
//            // 设置时间
            timeLable.text = status?.created_at
//
//            // 设置来源
            sourceLabel.text = status?.source
//
//            // 设置微博内容
            contentLable.text = status?.text
            
            // 设置认证图标
            verifiedTypeImage.image = status?.user?.verifiedImage
       // 设置会员等级
            memberView.image = status?.user?.mbrankImage
            
            
            // 判断是否需要显示配图
        
       statusOriginalViewBottomConstraint?.uninstall()
            if let picture = status?.pic_urls where picture.count > 0{
                // 有配图 不要隐藏
         picturesView.hidden = false
                
                picturesView.status  = status
               self.snp_makeConstraints(closure: { (make) -> Void in
         self.statusOriginalViewBottomConstraint = make.bottom.equalTo(picturesView).offset(XCStatusCellMargin).constraint
               })
                
            }else{
                // 不需要配图  隐藏配图
                picturesView.hidden = true
                
                self.snp_makeConstraints(closure: { (make) -> Void in

      self.statusOriginalViewBottomConstraint = make.bottom.equalTo(contentLable).offset(XCStatusCellMargin).constraint
              
                })
            }
            
        }
    }
    override init(frame: CGRect) {
        super.init(frame: frame)
      backgroundColor = UIColor.whiteColor()
        prepareUI()
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

    func prepareUI(){
        // 添加控件
        addSubview(IconView)
        addSubview(nameLable)
        addSubview(memberView)
        addSubview(timeLable)
        addSubview(sourceLabel)
        addSubview(verifiedTypeImage)
        addSubview(contentLable)
        addSubview(picturesView)
        // 添加约束
        // 头像
        IconView.snp_makeConstraints { (make) -> Void in
            make.leading.equalTo(self).offset(XCStatusCellMargin)
            make.top.equalTo(self).offset(XCStatusCellMargin)
            make.size.equalTo(CGSize(width: 35, height: 35))
        }
        // 昵称
        nameLable.snp_makeConstraints { (make) -> Void in
            make.leading.equalTo(IconView.snp_trailing).offset(XCStatusCellMargin)
            make.top.equalTo(IconView)
        }
        //时间
        timeLable.snp_makeConstraints { (make) -> Void in
            make.leading.equalTo(nameLable)
            make.bottom.equalTo(IconView)
        }
        // 来源
        sourceLabel.snp_makeConstraints { (make) -> Void in
            make.bottom.equalTo(IconView)
            make.leading.equalTo(timeLable.snp_trailing).offset(XCStatusCellMargin)
        }
        // 认证
        verifiedTypeImage.snp_makeConstraints { (make) -> Void in
            make.centerX.equalTo(IconView.snp_trailing)
            make.centerY.equalTo(IconView.snp_bottom)
        }
        // 内容
        contentLable.snp_makeConstraints { (make) -> Void in
            make.trailing.equalTo(self).offset(-XCStatusCellMargin)
            make.leading.equalTo(self).offset(XCStatusCellMargin)
            make.top.equalTo(IconView.snp_bottom).offset(XCStatusCellMargin)
        }
        // 微博配图
        picturesView.snp_makeConstraints { (make) -> Void in
            make.leading.equalTo(self).offset(XCStatusCellMargin)
            make.top.equalTo(contentLable.snp_bottom).offset(XCStatusCellMargin)
            make.size.equalTo(CGSizeMake(200, 200))
            
        }
        
        // 自身的约束
        self.snp_makeConstraints { (make) -> Void in
          self.statusOriginalViewBottomConstraint = make.bottom.equalTo(picturesView).offset(XCStatusCellMargin).constraint
        }
        
        

    }
    // MARK : - 懒加载
    /// 头像
    private lazy var IconView: UIImageView = UIImageView(image: UIImage(named: "avatar"))
    /// 昵称
    private lazy var nameLable:UILabel = UILabel(textColor: UIColor.darkGrayColor(), font: 14)
    /// 会员等级
    private lazy var memberView:UIImageView  = UIImageView(image: nil)
    /// 时间
    private lazy var timeLable:UILabel = UILabel(textColor: UIColor.orangeColor(), font: 11)
    /// 来源
    private lazy var sourceLabel:UILabel = UILabel(textColor: UIColor.lightGrayColor(), font: 11)
    /// 认证
    private lazy var verifiedTypeImage = UIImageView(image: nil)
    /// 内容
    private lazy var contentLable:UILabel = {
        let lable = UILabel(textColor: UIColor.darkGrayColor(), font: 15)
        lable.numberOfLines = 0
        return lable
        }()
   /// 微博视图
    lazy var picturesView:XCStatusPictureView = XCStatusPictureView()

    
}
